package com.elysium.template;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.elysium.properties.AppSettingProperties;

public class AppWeChatTemplate {

    private AppSettingProperties appSettingProperties;


    public AppWeChatTemplate(AppSettingProperties appSettingProperties) {
        this.appSettingProperties = appSettingProperties;
    }

    /**
     * 根据code获取accessToken
     * @return
     */
    public String getAccessToken(){
        // 1.获取access_token
        String accessTokenUrl = String.format(appSettingProperties.getAccessTokenUrl(), appSettingProperties.getAppId(), appSettingProperties.getSecret());
        String resultJsonStr = HttpUtil.get(accessTokenUrl);
        // 用Map  或者 JsonObject
        // 2.把字符串Json结果转换为JsonObject对象，方便获取值
        JSONObject accessTokenResult = JSONUtil.toBean(resultJsonStr, JSONObject.class);
        // 3.拿到accessToken，并且判断是否为空，如果为空抛出异常
        String accessToken = accessTokenResult.getStr("access_token");
        if (StrUtil.isEmpty(accessToken)){
            throw new RuntimeException("获取access_token失败！");
        }
        return accessToken;
    }


    /**
     * 小程序获取微信用户手机号
     * @param code
     * @return
     */
    public String getPhoneByAccessTokenAndCode(String code){
        String accessToken = this.getAccessToken();
        // 4.准备获取手机号的url,accessToken是路径参数
        String phoneUrl = String.format(appSettingProperties.getPhoneUrl(), accessToken);
        // 5.准备body参数
//        HashMap<String, Object> body = new HashMap<>();
//        body.put("code", phoneCode);
        JSONObject body = new JSONObject();
        body.set("code", code);

        String resultPhoneStr = HttpUtil.post(phoneUrl, body.toString());

        // 6.把jsonStr结果转换为JsonObj对象
        JSONObject resultJsonObj = JSONUtil.parseObj(resultPhoneStr);
        // 7.拿到errcode判断是否为0，如果不是那么就抛出异常
        Integer errcode = resultJsonObj.getInt("errcode");
        if (0 != errcode){
            throw new RuntimeException("获取手机号失败！");
        }

        // 8.获取到手机号对象，从对象中获取到手机号
        JSONObject phoneInfoJsonObj = resultJsonObj.getJSONObject("phone_info");
        String phoneNumber = phoneInfoJsonObj.getStr("phoneNumber");
        if (StrUtil.isEmpty(phoneNumber)){
            throw new RuntimeException("获取手机号失败！");
        }

        return phoneNumber;
    }


    /**
     * 获取小程序用户的openId
     * @param code
     * @return
     */
    public String getOpenIdByCode(String code){
        // 1.先根据openIdCode拿到openId
        String url = String.format(appSettingProperties.getOpenIdUrl(), appSettingProperties.getAppId(), appSettingProperties.getSecret(), code);
        // 发起 HTTP 请求，获取微信服务器返回的 JSON 格式的结果
        String jsonResultStr = HttpUtil.get(url);
        if (StrUtil.isEmpty(jsonResultStr)){
            throw new RuntimeException("获取openId失败！");
        }
        JSONObject resultJsonObj = JSONUtil.parseObj(jsonResultStr);

        String openId = resultJsonObj.getStr("openid");
        if (StrUtil.isEmpty(openId)){
            throw new RuntimeException("获取openId失败！");
        }

        return openId;
    }

}